﻿<UserControl xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"  x:Class="DemoNYTimes.BestsellersPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:charting="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:datavis="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"          
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" 
    xmlns:nyt="clr-namespace:NYTimes;assembly=NYTimesSilverlightKit"
    xmlns:nytvc="clr-namespace:NYTimes.ValueConverters;assembly=NYTimesSilverlightKit"              
    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"              
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <UserControl.Resources>
        <nyt:Bestsellers d:IsDataSource="true" x:Key="BestSellers"/>
        <nyt:Dates x:Key="Dates" d:IsDataSource="true"/>
        <nytvc:AxisSwapConverter x:Key="AxisSwapConverter"/>
        <nytvc:BestsellerHistoryConverter x:Key="BestsellerHistoryConverter"/>
        <nytvc:CollapsedIfNullConverter x:Key="CollapsedIfNullConverter"/>
        <nytvc:NumericFormatConverter x:Key="NumericFormatConverter"/>
        <nytvc:VisibleIfTrueConverter x:Key="VisibleIfTrueConverter"/>
        <!-- NoTickMarkStyle -->
        <Style x:Key="NoTickMarkStyle" TargetType="Line">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
        <!-- AxisLabelStyle -->
        <Style x:Key="AxisLabelStyle" TargetType="charting:AxisLabel">
            <Setter Property="Visibility" Value="Collapsed"/>
        </Style>
        <!-- SparklineLegendItemStyle -->
        <Style x:Key="SparklineLegendItemStyle" TargetType="charting:LegendItem">
            <Setter Property="Visibility" Value="Visible"/>
        </Style>
        <!-- LineDataPointStyle1 -->
        <Style x:Key="LineDataPointStyle1" TargetType="charting:LineDataPoint">
            <Setter Property="Background" Value="Orange"/>
            <Setter Property="BorderBrush" Value="Gray"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="IsTabStop" Value="False"/>
            <!--
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="charting:LineDataPoint">
                        <Grid x:Name="Root" Opacity="0">
                            <vsm:VisualStateManager.VisualStateGroups>
                                <vsm:VisualStateGroup x:Name="CommonStates">
                                    <vsm:VisualStateGroup.Transitions>
                                        <vsm:VisualTransition GeneratedDuration="0:0:0.1"/>
                                    </vsm:VisualStateGroup.Transitions>
                                    <vsm:VisualState x:Name="Normal"/>
                                    <vsm:VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="MouseOverHighlight" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                                <SplineColorKeyFrame KeyTime="00:00:00" Value="#FFFFDF00"/>
                                            </ColorAnimationUsingKeyFrames>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="MouseOverHighlight" Storyboard.TargetProperty="(UIElement.Opacity)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.24"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </vsm:VisualState>
                                </vsm:VisualStateGroup>
                                <vsm:VisualStateGroup x:Name="SelectionStates">
                                    <vsm:VisualStateGroup.Transitions>
                                        <vsm:VisualTransition GeneratedDuration="0:0:0.1"/>
                                    </vsm:VisualStateGroup.Transitions>
                                    <vsm:VisualState x:Name="Unselected"/>
                                    <vsm:VisualState x:Name="Selected">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="SelectionHighlight" Storyboard.TargetProperty="(UIElement.Opacity)">
                                                <SplineDoubleKeyFrame KeyTime="00:00:00" Value="0.18"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </vsm:VisualState>
                                </vsm:VisualStateGroup>
                                <vsm:VisualStateGroup x:Name="RevealStates">
                                    <vsm:VisualStateGroup.Transitions>
                                        <vsm:VisualTransition GeneratedDuration="0:0:0.5"/>
                                    </vsm:VisualStateGroup.Transitions>
                                    <vsm:VisualState x:Name="Shown">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="1"/>
                                        </Storyboard>
                                    </vsm:VisualState>
                                    <vsm:VisualState x:Name="Hidden">
                                        <Storyboard>
                                            <DoubleAnimation Duration="0" Storyboard.TargetName="Root" Storyboard.TargetProperty="Opacity" To="0"/>
                                        </Storyboard>
                                    </vsm:VisualState>
                                </vsm:VisualStateGroup>
                            </vsm:VisualStateManager.VisualStateGroups>
                            <ToolTipService.ToolTip>
                                <ContentControl Content="{TemplateBinding FormattedDependentValue}"/>
                            </ToolTipService.ToolTip>
                            <Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}"/>
                            <Ellipse RenderTransformOrigin="0.661,0.321">
                                <Ellipse.Fill>
                                    <RadialGradientBrush GradientOrigin="0.681,0.308">
                                        <GradientStop Color="#00FFFFFF"/>
                                        <GradientStop Color="#FF3D3A3A" Offset="1"/>
                                    </RadialGradientBrush>
                                </Ellipse.Fill>
                            </Ellipse>
                            <Ellipse x:Name="SelectionHighlight" Opacity="0" Fill="Red"/>
                            <Ellipse x:Name="MouseOverHighlight" Opacity="0" Fill="White"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
                
            </Setter>
            -->
            <Setter Property="DependentValueStringFormat" Value="{}{0:##;##}"/>
        </Style>
        <!-- ReviewTemplate -->
        <DataTemplate x:Key="ReviewTemplate">
            <Grid>
                <StackPanel Margin="5,0,0,0">
                    <HyperlinkButton Content="Book Review" TargetName="_blank" NavigateUri="{Binding Mode=OneWay, Path=BookReview}" Visibility="{Binding Converter={StaticResource CollapsedIfNullConverter}, Path=BookReview}"/>
                    <HyperlinkButton Content="First Chapter" TargetName="_blank" NavigateUri="{Binding Mode=OneWay, Path=FirstChapter}" Visibility="{Binding Converter={StaticResource CollapsedIfNullConverter}, Path=FirstChapter}"/>
                    <HyperlinkButton Content="Sunday Review" TargetName="_blank" NavigateUri="{Binding Mode=OneWay, Path=SundayReview}" Visibility="{Binding Converter={StaticResource CollapsedIfNullConverter}, Path=SundayReview}"/>
                    <HyperlinkButton Content="Article Chapter" TargetName="_blank" NavigateUri="{Binding Mode=OneWay, Path=ArticleChapter}" Visibility="{Binding Converter={StaticResource CollapsedIfNullConverter}, Path=ArticleChapter}"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
        <Style x:Key="NoLegendStyle" TargetType="datavis:Legend">
        	<Setter Property="Width" Value="0"/>
			<Setter Property="Height" Value="0"/>
        </Style>

        <!-- BookTemplate -->
        <DataTemplate x:Key="BookTemplate">
            <Grid Width="400">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Rectangle Grid.Column="0" Grid.ColumnSpan="3" Stroke="#FF000000" RadiusX="4" RadiusY="4" Fill="#FFEBE8CF"/>
                <TextBlock HorizontalAlignment="Left" Margin="3,3,7,3" VerticalAlignment="Center" Grid.Column="0" FontFamily="Times New Roman" FontSize="24" Text="{Binding Mode=OneWay, Path=Rank.Rank}" TextWrapping="Wrap" x:Name="Rank"/>
                <StackPanel Margin="3,3,3,3" Grid.Column="1">
                    <TextBlock FontSize="16" FontStyle="Italic" Text="{Binding Mode=OneWay, Path=Details.Title}" FontFamily="Times New Roman" x:Name="Title" TextWrapping="Wrap"/>
                    <TextBlock FontSize="14" Text="{Binding Mode=OneWay, Path=Details.Contributor}" FontFamily="Times New Roman" x:Name="Contributor" TextWrapping="Wrap"/>
                    <TextBlock FontSize="12" FontFamily="Times New Roman" Text="{Binding Path=Details.Description}" TextWrapping="Wrap" x:Name="Description" />
                    <controls:HeaderedItemsControl Height="Auto" Width="Auto" x:Name="Reviews" Header="Reviews" ItemsSource="{Binding Mode=OneWay, Path=Reviews}" ItemTemplate="{StaticResource ReviewTemplate}" FontFamily="Trebuchet MS" FontWeight="Bold" FontSize="14"/>
                    <TextBlock FontSize="14" Foreground="Green" Text="{Binding Converter={StaticResource NumericFormatConverter}, ConverterParameter=C2, Path=Details.Price}"/>
                </StackPanel>
                <StackPanel Grid.Column="2" >
                    <StackPanel.Resources>
                        <nyt:BestsellerHistory x:Key="History" ISBN="1416562850"/>
                    </StackPanel.Resources>
                    <charting:Chart BorderBrush="{x:Null}" LegendStyle="{StaticResource NoLegendStyle}" Title="Rank" FontFamily="Trebuchet MS" >
                        <charting:Chart.Axes>
                            <charting:LinearAxis MajorTickMarkStyle="{StaticResource NoTickMarkStyle}" Orientation="Y" AxisLabelStyle="{StaticResource AxisLabelStyle}" Minimum="-35" Maximum="1"/>
                            <charting:DateTimeAxis MajorTickMarkStyle="{StaticResource NoTickMarkStyle}"  Orientation="X" AxisLabelStyle="{StaticResource AxisLabelStyle}"
                                                   Minimum="{Binding Path=OffsetDate, Source={StaticResource Dates}}"
                                                   Maximum="{Binding Path=Now, Source={StaticResource Dates}}"/>
                        </charting:Chart.Axes>
                        <charting:Chart.Series>
                            <charting:LineSeries 
                                DataContext="{Binding Converter={StaticResource BestsellerHistoryConverter}}" 
                                ItemsSource="{Binding Result.RanksHistory}"  
                                DependentValueBinding="{Binding Rank,Converter={StaticResource AxisSwapConverter}}" 
                    		    IndependentValueBinding="{Binding PublishedDate}" DataPointStyle="{StaticResource LineDataPointStyle1}" LegendItemStyle="{StaticResource SparklineLegendItemStyle}"/>
                        </charting:Chart.Series>
                    </charting:Chart>
                </StackPanel>
                <StackPanel Height="Auto" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="Auto" Grid.Column="1" Orientation="Horizontal">
                    <TextBlock ToolTipService.ToolTip="This book's sales are barely distinguishable from those of the book above it on the list." Visibility="{Binding Converter={StaticResource VisibleIfTrueConverter}, Path=Rank.Asterisk}" Text="*" TextWrapping="Wrap" FontSize="36" FontFamily="Times New Roman" x:Name="Asterisk"/>
                    <TextBlock ToolTipService.ToolTip="Some bookstores have received bulk orders for this book." Visibility="{Binding Converter={StaticResource VisibleIfTrueConverter}, Path=Rank.Dagger}" Text="†" TextWrapping="Wrap" x:Name="Dagger" FontFamily="Times New Roman" FontSize="36"/>
                </StackPanel>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ListBox DataContext="{StaticResource BestSellers}" ItemsSource="{Binding Path=ListNames}" SelectedItem="{Binding Mode=TwoWay, Path=ListName}" HorizontalAlignment="Left" Width="Auto" x:Name="ListNames" />
        <ListBox DataContext="{StaticResource BestSellers}" ItemsSource="{Binding Path=List}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemTemplate="{StaticResource BookTemplate}" Grid.Column="1" x:Name="Books"/>
        <Grid Height="Auto" HorizontalAlignment="Stretch" Margin="0,0,0,0" VerticalAlignment="Stretch" Width="Auto" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" x:Name="BestSellersFooter">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock VerticalAlignment="Top" Text="Weeks to Show" TextWrapping="NoWrap" Margin="0,0,6,0" FontFamily="Trebuchet MS" FontSize="14" d:LayoutOverrides="Width"/>
            <Slider HorizontalAlignment="Stretch" VerticalAlignment="Top" Maximum="52" Minimum="1" LargeChange="4" SmallChange="1" Value="{Binding Mode=TwoWay, Path=Weeks, Source={StaticResource Dates}}" Height="28" Margin="0,0,0,0" Grid.Column="1"/>
            <TextBlock HorizontalAlignment="Left" x:Name="Weeks" VerticalAlignment="Top" TextWrapping="Wrap" Text="{Binding Converter={StaticResource NumericFormatConverter}, ConverterParameter=0, Mode=OneWay, Path=Weeks, Source={StaticResource Dates}}" FontFamily="Trebuchet MS" FontSize="14" Grid.Column="2"/>
            <TextBlock Text="* This book's sales are barely distinguishable from those of the book above it on the list." TextWrapping="Wrap" FontFamily="Trebuchet MS" FontSize="14" Height="Auto" VerticalAlignment="Bottom" Grid.ColumnSpan="3" Grid.Row="1"/>
            <TextBlock Text="† Some bookstores have received bulk orders for this book." TextWrapping="Wrap" FontFamily="Trebuchet MS" FontSize="14" Height="Auto" VerticalAlignment="Bottom" Grid.ColumnSpan="3" Grid.Row="2"/>
        </Grid>
    </Grid>
</UserControl>
